package com.android.activity.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.R;
import com.android.bean.Good;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.FutureTarget;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> {
    private ArrayList<Good> goodArrayList;
    private Context context;

    public GoodsAdapter(ArrayList<Good> goodArrayList,Context context) {
        this.goodArrayList = goodArrayList;
        this.context = context;
        Log.e("goodsAdapter","GoodsAdapter");
    }

    @NonNull
    @Override
    public GoodsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.goods_show_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsAdapter.ViewHolder holder, int position) {
        String strTvTitle;
        String strTvCount;
        String imageUrl;

        if (goodArrayList.get(position) != null){
            strTvTitle = goodArrayList.get(position).getContent();
            strTvCount = goodArrayList.get(position).getPrice().toString();
            imageUrl = (String) goodArrayList.get(position).getImageUrlList().get(0);
            holder.getTvTitle().setText(strTvTitle);
            holder.getTvCount().setText(strTvCount);

            Glide.with(context)
                    .load(imageUrl)
                    .into(holder.getIvGoods());
        }
    }

    @Override
    public int getItemCount() {
        if (goodArrayList != null)
        return goodArrayList.size();

        return 0;
    }

    public  class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivGoods;
        private  TextView tvTitle;
       private  TextView tvCount;
       public ViewHolder(@NonNull View itemView) {
            super(itemView);
           ivGoods = itemView.findViewById(R.id.iv_goods);
           tvTitle = itemView.findViewById(R.id.tv_title);
           tvCount = itemView.findViewById(R.id.tv_count);
        }

        public ImageView getIvGoods() {
            return ivGoods;
        }

        public TextView getTvTitle() {
            return tvTitle;
        }

        public TextView getTvCount() {
            return tvCount;
        }
    }
}
